package Demo.demo6;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class ExceptionDemo {
    public static void a() {
        a();
    }

    public static void b() {
        for (int i = 0; i <= 999999999; i++) {
            System.out.println(i);
        }
    }

    public static void c() {
        long[] arrary = new long[1024 * 1024 * 400];
    }

    //数组越界异常
    public static void arrayDemo() {
        int[] a = {1, 2, 3};
        try {
            for (int i = 0; i <= a.length; i++) {
                System.out.println(a[i]);
            }
        } catch (Exception e) {
            System.out.println(e.getCause());
            System.out.println(e.getMessage());
        }
    }

    //演示一下检查异常
    public void testCheck() {
        FileInputStream fis = null;
        String strDir = "";
        strDir=this.getClass().getResource("").getPath();
        System.out.println(strDir);
        try {
         fis = new FileInputStream(strDir+"1.txt");
            fis.close();
        } catch (FileNotFoundException e1) {
            System.out.println("e1");
        } catch (IOException e2) {
            System.out.println("e2");
        } catch (Exception e3) {
            System.out.println("e3");
        } finally {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }catch(NullPointerException e4){

            }
        }
        System.out.println("testCheck");
    }


    public static void main(String[] args) {
        // main(args);
        //  a();
        //  b();
        // c();
        arrayDemo();
        ExceptionDemo ed = new ExceptionDemo();
        ed.testCheck();
    }
}
